package com.lvan.fileupload.utils;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;

/**
 * @author Lvan
 */
@Component
public class ConstantJarPath implements InitializingBean {

    /**
     * 绝对路径
     **/
    private String absolutePath;

    /**
     * 静态目录
     **/
    private String staticDir = "static";

    /**
     * 文件存放的目录
     **/
    private String fileDir = "/upload/";

    public static String ABSOLUTE_PATH;
    public static String STATIC_DIR;
    public static String FILE_DIR;

    @Override
    public void afterPropertiesSet() throws Exception {
        File file = null;
        try {
            file = new File(ResourceUtils.getURL("classpath:").getPath());
        } catch (FileNotFoundException e) {
            throw new RuntimeException("获取根目录失败，无法创建上传目录！");
        }
        if (!file.exists()) {
            file = new File("");
        }
        absolutePath = file.getAbsolutePath();
        File upload = new File(absolutePath, staticDir + fileDir);
        if (!upload.exists()) {
            upload.mkdirs();
        }

        ABSOLUTE_PATH = absolutePath;
        STATIC_DIR = staticDir;
        FILE_DIR = fileDir;
    }
}
